package com.uwantsoft.business.service;

import com.uwantsoft.business.facade.dto.user.DriverLoginDto;
import com.uwantsoft.business.facade.dto.user.DriverOverviewQueryCondition;
import com.uwantsoft.business.service.domain.addresses.FavoriteAddress;
import com.uwantsoft.business.service.domain.user.Driver;
import com.uwantsoft.business.service.domain.user.LicensePlate;
import com.uwantsoft.business.service.infrastructure.DriverRepository;
import com.uwantsoft.constant.Constant;
import com.uwantsoft.util.SecurityUtils;
import com.uwantsoft.util.smssend.SmsSender;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author LeiPeichen.
 */
@Service(value = "driverService")
public class DriverServiceImpl {
    @Resource(name = "hibernateDriverRepository")
    private DriverRepository hibernateDriverRepository;

    public void login(DriverLoginDto driverLoginDto) {
        Driver driver = hibernateDriverRepository.findByGuid(SecurityUtils.currentUser().getGuid(), Driver.class);
        driver.login(driverLoginDto);
    }

    public void sendVerifyCode(String mobileNumber) {
        Driver driver = hibernateDriverRepository.findByPhoneNumber(mobileNumber);
        if (driver == null) {
            driver = Driver.createDriver(mobileNumber);
        }
        String verifyCode = (int)((Math.random()*9+1)*100000)+"";
//        String result = SmsSender.sendSms(Constant.SMS_URL, Constant.SMS_USERID, Constant.SMS_USERNAME, Constant.SMS_PASSWORD, mobileNumber, verifyCode);
//        if(!Constant.SMS_REUID_ERROR.equals(result)){
            driver.updateVerifyCode(verifyCode);
            hibernateDriverRepository.save(driver);
//        }
    }

    public FavoriteAddress createFavoriteAddress(FavoriteAddress favoriteAddress){
        Driver driver = hibernateDriverRepository.findByGuid(SecurityUtils.currentUser().getGuid(), Driver.class);
        driver.createFavoriteAddress(favoriteAddress);
        return favoriteAddress;
    }

    public List<FavoriteAddress> obtainFavoriteAddresses(){
        Driver driver = hibernateDriverRepository.findByGuid(SecurityUtils.currentUser().getGuid(), Driver.class);
        return driver.favoriteAddresses();
    }

    public void deleteFavoriteAddress(String guid) {
        Driver driver = hibernateDriverRepository.findByGuid(SecurityUtils.currentUser().getGuid(), Driver.class);
        driver.deleteFavoriteAddress(guid);
    }

    public List<LicensePlate> obtainLicensePlates() {
        Driver driver = hibernateDriverRepository.findByGuid(SecurityUtils.currentUser().getGuid(), Driver.class);
        return driver.licensePlates();
    }

    public void createLicensePlate(LicensePlate licensePlate) {
        Driver driver = hibernateDriverRepository.findByGuid(SecurityUtils.currentUser().getGuid(), Driver.class);
        driver.createLicensePlate(licensePlate);
    }

    /**
     * when try to set current license plate as priority using ,pre-priority license plate set to be false.
     * Because for one driver, only one priority-using license plate
     * @param guid current license plate guid
     */
    public void updatePriorityLicensePlate(String guid) {
        LicensePlate priorityLicensePlate = hibernateDriverRepository.findPriorityLicensePlate();
        LicensePlate licensePlate = hibernateDriverRepository.findByGuid(guid, LicensePlate.class);
        if(licensePlate != null){
            licensePlate.updatePriorityLicense(priorityLicensePlate);
        }
    }

    public void deleteLicensePlate(String guid) {
        Driver driver = hibernateDriverRepository.findByGuid(SecurityUtils.currentUser().getGuid(), Driver.class);
        driver.deleteLicensePlate(guid);
    }

    public List<Driver> obtainDrivers(DriverOverviewQueryCondition condition){
        return hibernateDriverRepository.findDrivers(condition);
    }

    public int obtainDriverTotalAmount(DriverOverviewQueryCondition condition){
        return hibernateDriverRepository.findDriverTotalAmount(condition);
    }

    public void lockDriver(String guid, boolean lock) {
        Driver driver = hibernateDriverRepository.findEntityByGuid(guid, Driver.class);
        driver.lock(lock);
    }

    public Driver obtainDriver(String guid) {
        return hibernateDriverRepository.findEntityByGuid(guid,Driver.class);
    }
}
